package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.ScmStockFrozenDTO;
import com.xinsoft.entity.dto.ScmStockParam;
import com.xinsoft.entity.po.ScmStockFrozen;
import com.xinsoft.service.ScmStockFrozenService;
import com.xinsoft.mapper.ScmStockFrozenMapper;
import com.xinsoft.service.ScmStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ZYM
* @description 针对表【scm_stock_frozen(库存冻结明细)】的数据库操作Service实现
* @createDate 2023-08-15 15:42:43
*/
@Service
public class ScmStockFrozenServiceImpl extends ServiceImpl<ScmStockFrozenMapper, ScmStockFrozen>
    implements ScmStockFrozenService{

    @Autowired
    private ScmStockService scmStockService;

    /**
     * 分页
     * @param param
     * @return
     */
    @Override
    public List<ScmStockFrozenDTO> findListByParam(ScmStockParam param) {
        //分页
        List<ScmStockFrozenDTO> result = this.baseMapper.findListByParam(param);
        //返回值
        return result;
    }

    /**
     * 合计行
     * @param param
     * @return
     */
    @Override
    public ScmStockFrozenDTO findTotalByParam(ScmStockParam param) {

        ScmStockFrozenDTO total = this.baseMapper.findTotalByParam(param);
        //返回值
        return total;
    }

    /**
     * 增加
     * @param scmStockFrozen
     * @return
     */
    @Override
    public ScmStockFrozen addMesStation(ScmStockFrozen scmStockFrozen) {

        save(scmStockFrozen);
        //更新冻结库存
        UpdateWrapper scmStockUpdateWrapper = new UpdateWrapper();
        scmStockUpdateWrapper.setSql("batch_num_frozen = batch_num_frozen + "+ scmStockFrozen.getBatchNumFrozen().toString());
        scmStockUpdateWrapper.eq("id",scmStockFrozen.getStockId());
        scmStockService.update(scmStockUpdateWrapper);

        return scmStockFrozen;
    }

    /**
     * 编辑
     * @param scmStockFrozen
     * @return
     */
    @Override
    public ScmStockFrozen editMesStation(ScmStockFrozen scmStockFrozen) {
        ScmStockFrozen entity = getById(scmStockFrozen.getStockId());
        updateById(scmStockFrozen);
        //更新冻结库存
        UpdateWrapper scmStockUpdateWrapper = new UpdateWrapper();
        scmStockUpdateWrapper.setSql("batch_num_frozen = batch_num_frozen - "+ (scmStockFrozen.getBatchNumFrozen().subtract(entity.getBatchNumFrozen())).toString());
        scmStockUpdateWrapper.eq("id",scmStockFrozen.getStockId());
        scmStockService.update(scmStockUpdateWrapper);
        return scmStockFrozen;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public void del(List<Integer> ids) {

        //更新冻结库存
        for (Integer id : ids) {
            ScmStockFrozen entity = getById(id);
            UpdateWrapper scmStockUpdateWrapper = new UpdateWrapper();
            scmStockUpdateWrapper.setSql("batch_num_frozen = batch_num_frozen + "+ entity.getBatchNumFrozen().toString());
            scmStockUpdateWrapper.eq("id",entity.getStockId());
            scmStockService.update(scmStockUpdateWrapper);
        }

        this.removeByIds(ids);
    }


}




